package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.service.EduChapterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 课程章节 前端控制器
 * </p>
 *
 * @author hwt
 * @since 2022-08-23
 */
@RestController
@RequestMapping("eduservice/edu-chapter")
public class EduChapterController {

    @Resource
    private EduChapterService chapterService;

    // 课程大纲列表，根据课程 id 进行查询所有的章节、小结
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable("courseId") String courseID) {
        return R.ok().data("allChapterVideo", chapterService.getChapterVideoByCourseId(courseID));
    }


    // 添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter chapter) {
        chapterService.save(chapter);
        return R.ok();
    }

    // 根据 id 查询章节
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        return R.ok().data("chapter", chapterService.getById(chapterId));
    }

    // 修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    // 删除章节
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        // 删除章节有两种方法：
        //     删除章节时，删除所有小结
        //     删除时，要先删除小结，小结为空，才允许删除（此处采用方案）
        return chapterService.deleteChapter(chapterId) ? R.ok() : R.error();
    }

}

